package engine;

import graphics.Window;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;


import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.text.NumberFormatter;

import movables.Snake;

public class StarterWindow implements ActionListener{

	JFormattedTextField SQsize, snakes, numberWalls, numberFood, numberBoost, numberOfSquarePixies;
	JFrame newGame;
	JCheckBox randComponents, music;
	int count= 0, squarePixies;
	JButton start;
	JButton startTest;


	public void init(){
		newGame = new JFrame("Options");
		newGame.setLocation(500, 200);
		newGame.setLayout(new GridLayout(10,1,0,2));
		newGame.setSize(300, 350);

		randComponents = new JCheckBox(" - Generate random");
		randComponents.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (randComponents.isSelected()){
					numberFood.setEnabled(false);
					numberWalls.setEnabled(false);
					numberBoost.setEnabled(false);
				}

				else{
					numberFood.setEnabled(true);
					snakes.setEnabled(true);
					numberWalls.setEnabled(true);
					numberBoost.setEnabled(true);
				}
			}
		});


		NumberFormat customFormat = NumberFormat.getIntegerInstance();
		customFormat.setMaximumFractionDigits(0);

		newGame.add(new JLabel("Square Size: "));
		SQsize = new JFormattedTextField(new NumberFormatter(customFormat));
		SQsize.setValue(30);
		newGame.add(SQsize);
		
		newGame.add(new JLabel("Square Pixies: "));
		numberOfSquarePixies = new JFormattedTextField(new NumberFormatter(customFormat));
		numberOfSquarePixies.setValue(20);
		newGame.add(numberOfSquarePixies);
		

		newGame.add(new JLabel("Number of snakes: "));
		snakes = new JFormattedTextField(new NumberFormatter(customFormat));
		snakes.setValue(10);
		newGame.add(snakes);

		newGame.add(randComponents);
		newGame.add(new JLabel("number of components?"));

		newGame.add(new JLabel("Number of walls: "));
		numberWalls = new JFormattedTextField(new NumberFormatter(customFormat));
		numberWalls.setValue(100);
		newGame.add(numberWalls);

		newGame.add(new JLabel("Number of food: "));
		numberFood = new JFormattedTextField(new NumberFormatter(customFormat));
		numberFood.setValue(15);
		newGame.add(numberFood);

		newGame.add(new JLabel("Number of boost: "));
		numberBoost = new JFormattedTextField(new NumberFormatter(customFormat));
		numberBoost.setValue(10);
		newGame.add(numberBoost);

		start = new JButton("Start");
		start.addActionListener(this);
		newGame.add(new JLabel("Start Game"));
		newGame.add(start);

		startTest = new JButton("Start Test");
		startTest.addActionListener(this);
		newGame.add(new JLabel("Test Concurrency"));
		newGame.add(startTest);

		music = new JCheckBox(" - Music");
		music.setSelected(true);
		newGame.add(music);
		
		newGame.setVisible(true);
		newGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		Board board;
		if (e.getSource().equals(start)){
			int auxSnakes = Integer.parseInt(snakes.getText());
			int numberOfWalls = Integer.parseInt(numberWalls.getText());
			int numberOfFood = Integer.parseInt(numberFood.getText());
			int numberOfBoost= Integer.parseInt(numberBoost.getText());
			boolean randomComponents = randComponents.isSelected();
			int squareSize = Integer.parseInt(SQsize.getText());
			squarePixies = Integer.parseInt(numberOfSquarePixies.getText());

			if (squarePixies>50)
				squarePixies = 50;
			else if (squarePixies<10)
				squarePixies = 10;
			
			if (squareSize>70)
				squareSize = 70;
			else if (squareSize<10)
				squareSize = 10;


			if (auxSnakes > (squareSize*2)/2)
				auxSnakes = (squareSize*2)/2;
			else if(auxSnakes<2)
				auxSnakes=2; 

			if(randomComponents){
				numberOfWalls = (int)(0.15 * (squareSize * squareSize));
				numberOfBoost = (int)(0.5 * auxSnakes + 0.5);
				numberOfFood = 0;
			}
			board = new Board(squareSize, numberOfWalls,numberOfFood,numberOfBoost, randomComponents);

			for (int x=0; x<auxSnakes; x++){

				Snake snake = new Snake(board, count);
				board.addSnake(snake);
				new Thread(snake).start();
				count++;
				if(count>3)
					count=0;
			}

		}

		else {
			board = new Board(30, 0, 0, 0, false);

			Snake snake1 = new Snake(board, new Point(-1,5), new Point(30,5));
			board.addSnake(snake1);
			new Thread(snake1).start();

			Snake snake2 = new Snake(board, new Point(30,5), new Point(-1,5));
			board.addSnake(snake2);
			new Thread(snake2).start();
			
			squarePixies = 20;
		}

		boolean musicState = false;
		if (music.isSelected())
			musicState = true;
		Window window = new Window(board, squarePixies, musicState);
		window.init();
		new Thread(window).start();
		newGame.setVisible(false);
	}
}
